package com.symaster.common.swing.element;

import com.symaster.common.thread.WatchThread;

import javax.swing.*;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class CanvasDisplay extends WatchThread {
    private final JComponent canvas;
    private final double fpsOffset;
    // public final List<Double> times = new LinkedList<>();

    public CanvasDisplay(JComponent canvas, double maxFps) throws IllegalArgumentException {
        super((int) Math.round(1000.0 / maxFps));
        this.canvas = canvas;
        this.fpsOffset = 1000.0 / maxFps;
    }

    @Override
    protected void execute() throws Exception {
        // double s = System.nanoTime() / 1000000.0;

        // SwingUtilities.invokeAndWait(canvas::repaint);
        // canvas.paint();
        // canvas.repaint();
        // canvas.paintImmediately(0, 0, canvas.getWidth(), canvas.getHeight());
        canvas.repaint();

        // double e = System.nanoTime() / 1000000.0;

        // double v = e - s;
        // times.add(v);

        // double v1 = fpsOffset - (e - s);
        // if (v1 <= 0) {
        //     return;
        // }
        // TimeUnit.MILLISECONDS.sleep((long) v1);
    }
}